﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.CApAssignmnetUtil
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.TcCommands;

internal static class CApAssignmnetUtil
{
  internal static void DisplayMessage(string defaultMessage)
  {
    string str = string.Join("\n", TxCallMessages.GetMessages());
    if (string.IsNullOrEmpty(str))
      str = defaultMessage;
    int num = (int) TxMessageBox.ShowModal(str, DnProcessSimulateCommands.TcCommands.TcNavigator.StringTable.SEARCH_MFG_COMMAND_NAME, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }

  internal static void CompleteContinuousMfgAssignment(
    TxMfgFeature mfg,
    TxTcOperation targetOperation)
  {
    TxObjectList directDescendants = targetOperation.GetDirectDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxContinuousRoboticOperation)));
    if (((Collection<ITxObject>) directDescendants).Count > 0)
    {
      ((TxBaseContinuousOperation) (((Collection<ITxObject>) directDescendants)[0] as TxContinuousRoboticOperation)).AssignMfgFeature((ITxObject) mfg);
    }
    else
    {
      TxContinuousRoboticOperationCreationData operationCreationData = new TxContinuousRoboticOperationCreationData(TxApplication.ActiveDocument.PhysicalRoot.GetUniqueDisplayName("ContinuousMfgOperation"));
      if (!targetOperation.CanCreateContinuousRoboticOperation(operationCreationData))
        return;
      ((TxBaseContinuousOperation) targetOperation.CreateContinuousRoboticOperation(operationCreationData)).AssignMfgFeature((ITxObject) mfg);
    }
  }

  internal static void CompleteMfgAssignment(
    TxTcImportResultEx result,
    ITxObject targetOperation,
    bool isPreview)
  {
    if (targetOperation == null || !(targetOperation is TxTcOperation) || result == null)
      return;
    if (isPreview)
      return;
    try
    {
      foreach (ITxObject itxObject in result.LoadedObjects.Select<TxTcResultObjectDataEx, ITxObject>((Func<TxTcResultObjectDataEx, ITxObject>) (x => x.Object)))
      {
        TxMfgFeature mfg = itxObject as TxMfgFeature;
        if (TxMfgFeature.op_Inequality(mfg, (ITxObject) null))
        {
          foreach (ITxObject assignedOperation in (Collection<ITxObject>) mfg.AssignedOperations)
          {
            if (mfg is TxSeamMfgFeature && assignedOperation == targetOperation)
            {
              (targetOperation as TxTcOperation).UnassignMfgFeature((ITxObject) mfg);
              CApAssignmnetUtil.CompleteContinuousMfgAssignment(mfg, targetOperation as TxTcOperation);
              break;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
  }

  internal static string GetMfgTypeFromBomLines(IEnumerable<string> uids)
  {
    string typeFromBomLines = "";
    if (uids.Count<string>() > 0)
    {
      try
      {
        List<string> typesFromBomLines = TxTcUtilities.GetObjectTypesFromBomLines(new List<string>(1)
        {
          uids.ElementAt<string>(0)
        });
        if (typesFromBomLines.Count == 1)
          typeFromBomLines = typesFromBomLines[0];
      }
      catch (Exception ex)
      {
        TxApplication.LogWriter.WriteExceptionLine(ex);
      }
    }
    return typeFromBomLines;
  }

  internal static TxPlanningTypeMetaData GetTypeMetaData(string typeStr)
  {
    TxPlanningTypeMetaData typeMetaData = (TxPlanningTypeMetaData) null;
    if (TxApplication.ActiveDocument.PlatformGlobalServicesProvider is ITxTcGlobalServicesProvider servicesProvider)
      typeMetaData = ((ITxPlatformGlobalServicesProvider) servicesProvider).GetTypeMetaData(typeStr);
    return typeMetaData;
  }

  internal static bool DoesTargetTypeMatchMfgType(ITxObject targetOperation, string mfgType)
  {
    bool flag = false;
    if (TxTcUtilities.IsClassDerivedFromClass(mfgType, "WeldPointRevision"))
    {
      int num;
      switch (targetOperation)
      {
        case TxTcDiscreteOperation _:
        case TxTcOperation _:
          num = 1;
          break;
        default:
          num = targetOperation is TxTcProcess ? 1 : 0;
          break;
      }
      flag = num != 0;
    }
    else if (TxTcUtilities.IsClassDerivedFromClass(mfgType, "ArcWeldRevision"))
    {
      int num;
      switch (targetOperation)
      {
        case TxTcContinuousOperation _:
        case TxTcOperation _:
          num = 1;
          break;
        default:
          num = targetOperation is TxTcProcess ? 1 : 0;
          break;
      }
      flag = num != 0;
    }
    if (flag)
    {
      TxPlanningTypeMetaData typeMetaData1 = TxTcAuthoring.GetTypeMetaData(targetOperation);
      if (typeMetaData1 != null)
        flag = TxTcAuthoring.CanBeAssignedToType(mfgType, typeMetaData1.TypeName);
      if (!flag && TxCallMessages.IsCollecting())
      {
        string str = "";
        TxPlanningTypeMetaData typeMetaData2 = CApAssignmnetUtil.GetTypeMetaData(mfgType);
        if (typeMetaData2 != null)
          str = typeMetaData2.DisplayName;
        if (string.IsNullOrEmpty(str))
          str = mfgType;
        TxCallMessages.Add("APIMSG_CANNOT_ASSIGN_MFG_TO_OPERATION", new object[2]
        {
          (object) str,
          (object) typeMetaData1.DisplayName
        });
      }
    }
    return flag;
  }

  internal static bool CanLoadMfgs(
    IEnumerable<string> uids,
    ITxObject targetOperation,
    bool isPreview)
  {
    bool flag = true;
    if (!isPreview)
    {
      using (new TxCallMessages.Collector((object) "CApAssignmnetUtil.CanLoadMfgs"))
      {
        string typeFromBomLines = CApAssignmnetUtil.GetMfgTypeFromBomLines(uids);
        if (uids.Count<string>() > 0)
        {
          if (!CApAssignmnetUtil.DoesTargetTypeMatchMfgType(targetOperation, typeFromBomLines))
          {
            flag = false;
            CApAssignmnetUtil.DisplayMessage(DnProcessSimulateCommands.TcCommands.TcNavigator.StringTable.SEARCH_MFG_TARGRT_MISMATCH_ERROR);
          }
        }
      }
    }
    return flag;
  }
}
